package com.liuxing.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.account.entity.User;
import com.liuxing.javaSpringBoot.modules.account.service.UserService;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File UserController.java
 * @Auth liuxing
 * @Date 2021/8/22 10:39
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * 127.0.0.1/api/login --- post
     * {"name":"liuxing7","password":"123456"}
     */
    @PostMapping(value = "/login",consumes = "application/json")
    public Result<User> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     * 用户登出
     * 127.0.0.1/api/logout --- get
     */
    @GetMapping("/logout")
    public Result<Object> logout(){
        return userService.logout();
    }

    /**
     * 添加用户信息
     * 127.0.0.1/api/user --- post
     * {"userName":"liuxing","userEmail":"liuxing997@foxmail.com","userPwd":"123456"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user){
        return userService.insertUser(user);
    }

    /**
     * 根据用户名查询用户信息
     * 127.0.0.1/api/user?name=liuxing7 --- get
     */
    @GetMapping("/user")
    public Result<Object> getUserByUsername(@RequestParam String name) {
        User user = userService.getUserByUsername(name);
        return new Result<>(Result.ResultCode.SUCCESS.code,"获取用户数据成功！",user);
    }
    /**
     * 根据用户ID修改用户信息
     * 127.0.0.1/api/user --- put
     * {"id":2,"userName":"liuxing","userEmail":"liuxing997@foxmail.com","userPwd":"123456"}
     */
    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updateUserById(@RequestBody User user){
        return userService.updateUserById(user);
    }

    /**
     * 根据用户ID删除用户
     * 127.0.0.1/api/user/2 --- delete
     */
    @DeleteMapping(value = "/user/{id}")
    @RequiresPermissions("/api/user/{id}")
    public Result<Object> deleteUserByUserId(@PathVariable int id) {
        return userService.deleteUserByUserId(id);
    }

    /**
     * 根据用户ID查询用户信息
     * 127.0.0.1/api/user/3 --- get
     */
    @GetMapping("/user/{id}")
    public Result<Object> getUserByUserId(@PathVariable int id) {
        User user = userService.getUserByUserId(id);
        return new Result<>(Result.ResultCode.SUCCESS.code, "查询成功！",user);
    }

    /**
     * 查询所有用户信息
     * 127.0.0.1/api/users --- get
     */
    @GetMapping("/users")
    public List<User> getAllUser() {
        return userService.getAllUser();
    }

    /**
     * 批量插入用户信息
     * 127.0.0.1/api/batch/users  --- post
     * [{"name":"liuxing5","email":"liuxing997@foxmail.com","password":"123456"},
     * {"name":"liuxing6","email":"liuxing997@foxmail.com","password":"123456"}]
     */
    @PostMapping(value = "/batch/users",consumes = "application/json")
    public Result<Object> insertUsers(@RequestBody List<User> users) {
        return userService.insertUsers(users);
    }

    /**
     * 分页查询用户信息
     * 127.0.0.1/api/users  --- post
     * {"currentPage":1,"pageSize":3,"orderBy":"name","direction":"desc"}
     */
    @PostMapping(value = "/users",consumes = "application/json")
    public PageInfo<User> getUsersBySearchBean(@RequestBody SearchBean searchBean) {
        return userService.getUsersBySearchBean(searchBean);
    }
}
